package org.boulderfoodrescue.webapp.client.old.viewer.scheduler;

import org.boulderfoodrescue.webapp.client.googlemap.GoogleMap;
import org.boulderfoodrescue.webapp.client.old.datamanager.DataManager;
import org.boulderfoodrescue.webapp.client.old.event.PlaceChangedEvent;
import org.boulderfoodrescue.webapp.client.old.viewer.SideBarPanel;
import org.boulderfoodrescue.webapp.client.pickuptimes.timepicker.DateTimePicker;
import org.boulderfoodrescue.webapp.client.pickuptimes.timepicker.PickupWindow;
import org.boulderfoodrescue.webapp.shared.Place;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

public class SchedulerPanel extends Composite{

  private static SchedulerPanelUiBinder uiBinder = GWT
      .create(SchedulerPanelUiBinder.class);

  interface SchedulerPanelUiBinder extends UiBinder<Widget, SchedulerPanel> {
  }

  @UiField
  Button button_save_address;
  @UiField
  Button button_cancel_save;
  @UiField
  Button add_time_interval;
  @UiField
  AvailableTimesCellTable times_table;
  private SideBarPanel parent;
  private Place current;
  private GoogleMap gMap;
  
  public SchedulerPanel(SideBarPanel parent, GoogleMap gMap) {
    initWidget(uiBinder.createAndBindUi(this));
    this.parent = parent;
    this.gMap = gMap;
  }
  
  public void setPlace(Place current){
  	this.current = current;
  }

  @UiHandler("button_save_address")
  public void onSaveClick(ClickEvent e){
//  	gMap.removeMarker(current);
    current.getPickup_times().addAll(times_table.getPickupTimes());
    times_table.getPickupTimes().clear();
    parent.pickupSaved();
    DataManager.get().fireEvent(new PlaceChangedEvent(current, true));
  }
  
  @UiHandler("button_cancel_save")
  public void onCancelClick(ClickEvent e){
  	times_table.getPickupTimes().clear();
  	parent.onCancelNewPickup();
  }
  
  @UiHandler("add_time_interval")
  public void onAddTimeClick(ClickEvent e){
//  	PickupWindow window = date_time.getPickupWindow();
//  	if(window.getEnd_time() == null || window.getStart_time() == null){
//  		Window.alert("The time interval you have selected is not valid. Please make sure that you format your time interval correctly");
//  		return;
//  	}
//  	current.getPickup_times().add(window);
//  	times_table.addPickupEvent(window);
  }
  
}
